//
//  OverlayInputViewController.m
//  UIViewControllerTransitionDemo
//
//  Created by Guo Rui on 2017/7/6.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "OverlayInputViewController.h"
#import <Masonry.h>
#import "OverlayAnimationController.h"
#import "OverlayPresentationController.h"

@interface OverlayInputViewController ()<UIViewControllerTransitioningDelegate>

@property (strong, nonatomic) UIButton *closeButton;
@property (strong, nonatomic) UITextField *textField;

@end

@implementation OverlayInputViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.transitioningDelegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupViews];
}

- (void)setupViews {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.closeButton.alpha = 0.f;
    [self.closeButton setImage:[UIImage imageNamed:@"photoBookClose"] forState:UIControlStateNormal];
    [self.closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.closeButton];
    
    self.textField = [[UITextField alloc] init];
    self.textField.placeholder = @"Place input";
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.textField.layer.borderWidth = 1.f;
    [self.view addSubview:self.textField];
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.top.offset(15);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.size.mas_equalTo(CGSizeMake(0, 40));
    }];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    CGFloat width = CGRectGetWidth(self.view.frame) - 2 * 15.f;
    [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
    }];
    
    [UIView animateWithDuration:.3 animations:^{
        self.closeButton.alpha = 1.f;
        [self.view layoutIfNeeded];
    }];
}

- (void)closeAction {
    
    CGAffineTransform transform = CGAffineTransformMakeRotation(3 * M_PI);
    transform = CGAffineTransformScale(transform, .1f, .1f);
    
    [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0);
    }];
    
    [UIView animateWithDuration:.3 animations:^{
        [self.view layoutIfNeeded];
        self.closeButton.transform = transform;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return [[OverlayAnimationController alloc] init];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [[OverlayAnimationController alloc] init];
}

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source {
    return [[OverlayPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}

- (void)dealloc {
    
    NSLog(@"\n************\n %@ dealloc \n************", self.class);
}


@end
